// Combinations.cpp : Defines the entry point for the console application.
//
/*
 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

For example,
If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

*/

#include "stdafx.h"
#include <vector>
using namespace std;
class Solution {
public:
    vector<vector<int> > combine(int n, int k) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<vector<int> > ret;
        if(n<=0)
            return ret;
        if(k<=0 || k>n)
            return ret;
        vector<int> combi;
        vector<int> num(n);

        for(int i = 1; i<=n; i++)
            num[i-1] = i;
        
        if(n== k)
        {
            ret.push_back(num);
            return ret;
        }
        
        HelpCombi(num, 0, k, combi, ret);
        return ret;
    }
    
    void HelpCombi(vector<int> num, int idx, int k, vector<int>& combi, vector<vector<int> >& ret)
    {
        if(k == 0)
        {
            ret.push_back(combi);
            return;
        }
        else
        {
            for(int i = idx; i<num.size(); i++)
            {
                combi.push_back(num[i]);
                HelpCombi(num, i+1, k-1, combi, ret);
                combi.pop_back();
            }
        }
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
	Solution s;
	s.combine(4, 2);
	return 0;
}

